#include once "fbgfx.bi"
#include once "includes\fbpng.bi"

declare function Pload(FNAME as string,ALPHAZERO as integer=0) as any ptr

' *************************************************************************************
' ********************************* LOADS A PNG FILE **********************************
' *************************************************************************************
function Pload(FNAME as string,ALPHAZERO as integer=1) as any ptr
  
  dim as any ptr TMP,DESTY
  dim as integer PX,PY,PXX
  dim as integer XX,YY
  dim as uinteger COR
  dim as uinteger ptr DSTY
  
  if dir$(FNAME) = "" then
    print #99,"File Not Found: ";FNAME
    sleep
    end
  end if
  
  TMP = Png_Load(FNAME,PNG_TARGET_FBNEW)      
  if TMP then
    PX = peek(integer,TMP+8)
    PY = peek(integer,TMP+12)
    DESTY = TMP
    if (PX mod 8) then 
      PX = (PX or 7)+1          
      DESTY = ImageCreate(PX,PY,rgba(255,0,255,0),MYBPP)        
      put DESTY,((PX-peek(integer,TMP+8))/2,0),TMP,pset
      ImageDestroy(TMP)    
    end if    
    PXX = (cptr(fb.image ptr,DESTY)->Pitch)/4
    if ALPHAZERO then
      DSTY = DESTY+32
      for CNT as integer = 0 to (PX*PY)-1    
        COR = DSTY[CNT] or &hFF000000
        if COR = &hFFFF00FF then 
          DSTY[CNT]=rgba(255,0,255,0)
        else
          DSTY[CNT]=COR
        end if
      next CNT
    end if
    return DESTY  
  end if
  return 0
  
end function
